package com.example.klaraservice.service.impl;

import com.example.klaraservice.dao.CommodityMapper;
import com.example.klaraservice.pojo.Commodity;
import com.example.klaraservice.pojo.User;
import com.example.klaraservice.service.CommodityService;
import com.example.klaraservice.utils.TimeUtil;
import com.example.klaraservice.utils.UrlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : Glacialerisa
 * @date : 2023/6/5 10:36
 * @desc :
 */
@Service
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    private CommodityMapper commodityMapper;
    @Override
    public Map<String, Object> selectAllLikeNameAndUid(int pageNum, int pageSize,String name, int id) {
        //通过PageHelper对象设定分页参数
        PageHelper.startPage(pageNum, pageSize);
        //当前页面的数据
        List<Commodity> list = commodityMapper.selectAllCommodityByUserId(name,id);
        for(Commodity commodity:list){
            String image = commodity.getImage();
//            System.out.println("原始地址："+image);
//            System.out.println("修改后的地址"+ip+image);
            commodity.setImage(UrlUtil.IP+image);
        }
        //创建PageInfo对象，并将当前页面数据传入，计算分页相关数据
        PageInfo<Commodity> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        //本页数据
        map.put("list", list);
        //总数据条目
        map.put("total", total);
        return map;
    }

    @Override
    public Map<String, Object> selectAllLikeName(int pageNum, int pageSize, String name) {
        //通过PageHelper对象设定分页参数
        PageHelper.startPage(pageNum, pageSize);
        //当前页面的数据
        List<Commodity> list = commodityMapper.selectCommodityByName(name);
        for(Commodity commodity:list){
            String image = commodity.getImage();
//            System.out.println("原始地址："+image);
//            System.out.println("修改后的地址"+ip+image);
            commodity.setImage(UrlUtil.IP+image);
        }
        //创建PageInfo对象，并将当前页面数据传入，计算分页相关数据
        PageInfo<Commodity> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        //本页数据
        map.put("list", list);
        //总数据条目
        map.put("total", total);
        return map;
    }

    @Override
    public boolean updateCommodityInfo(Commodity commodity) {
        commodity.setUpdateTime(TimeUtil.getTime());
        String imageUrl = commodity.getImage();
        String newUrl = UrlUtil.getUrl(imageUrl);
//        System.out.println(newUrl+"newUrl");
        commodity.setImage(newUrl);
        return commodityMapper.updateCommodity(commodity);
    }

    @Override
    public boolean addCommodity(Commodity commodity) {
        commodity.setUpdateTime(TimeUtil.getTime());
        String imageUrl = commodity.getImage();
        String newUrl = UrlUtil.getUrl(imageUrl);
        commodity.setImage(newUrl);
        return commodityMapper.addCommodity(commodity);
    }
}
